using System;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.MTS
{
	internal class TransXID
	{
		private const int ORAMTSTXNFORMAT = 1145324612;

		private const int MSDTCTXNFORMAT = 21255235;

		internal const int ORAXIDSIZE = 128;

		internal const int GUIDLEN = 16;

		internal const int MAXBRANCHGUILD_LEN = 64;

		internal static int BRANCHREQ_LEN;

		internal static int MAXRRECOHOSTNAME_LEN;

		internal OpoDTCTxnXIDRefCtx m_opoDTCTxnXID = new OpoDTCTxnXIDRefCtx();

		private Guid m_rmGuid;

		private Guid m_txnGuid;

		private int m_branchNum;

		static TransXID()
		{
			BRANCHREQ_LEN = 27;
			MAXRRECOHOSTNAME_LEN = 64 - BRANCHREQ_LEN;
		}

		private TransXID(int oraMTSFormatID, int MSDTCFormatID, Guid txnGuid, Guid rmGuid, int branchNum, string machineName, ushort port)
		{
			m_opoDTCTxnXID.m_formatID = oraMTSFormatID;
			m_txnGuid = txnGuid;
			m_rmGuid = rmGuid;
			m_branchNum = branchNum;
			byte[] bytes = new UTF8Encoding().GetBytes(machineName);
			byte[] array = txnGuid.ToByteArray();
			byte[] array2 = rmGuid.ToByteArray();
			byte[] bytes2 = BitConverter.GetBytes(port);
			byte[] bytes3 = BitConverter.GetBytes(m_branchNum);
			byte[] bytes4 = BitConverter.GetBytes(MSDTCFormatID);
			m_opoDTCTxnXID.m_gtrid_length = array.Length + 4;
			m_opoDTCTxnXID.m_bqual_length = array2.Length + bytes3.Length + bytes4.Length + 1 + bytes2.Length + bytes.Length;
			if (m_opoDTCTxnXID.m_bqual_length + m_opoDTCTxnXID.m_gtrid_length > 128)
			{
				int num = 128 - m_opoDTCTxnXID.m_gtrid_length - BRANCHREQ_LEN;
				if (bytes.Length > num)
				{
					throw new ConfigurationErrorsException(OracleStringResourceManager.GetErrorMesg(ResourceStringConstants.MTS_INVALID_CONFIG_VALUES));
				}
			}
			int num2 = 0;
			m_opoDTCTxnXID.m_data = new byte[128];
			array.CopyTo(m_opoDTCTxnXID.m_data, num2);
			num2 += m_opoDTCTxnXID.m_gtrid_length;
			array2.CopyTo(m_opoDTCTxnXID.m_data, num2);
			num2 += array2.Length;
			if (BitConverter.IsLittleEndian)
			{
				Array.Reverse(bytes3);
			}
			bytes3.CopyTo(m_opoDTCTxnXID.m_data, num2);
			num2 += bytes3.Length;
			bytes4.CopyTo(m_opoDTCTxnXID.m_data, num2);
			num2 += bytes4.Length;
			num2++;
			if (BitConverter.IsLittleEndian)
			{
				Array.Reverse(bytes2);
			}
			bytes2.CopyTo(m_opoDTCTxnXID.m_data, num2);
			num2 += bytes2.Length;
			bytes.CopyTo(m_opoDTCTxnXID.m_data, num2);
		}

		~TransXID()
		{
		}

		internal static TransXID CreateOracleXID(Guid txnGuid, Guid rmGuid, int branchNum)
		{
			return new TransXID(1145324612, 21255235, txnGuid, rmGuid, branchNum, ConfigBaseClass.m_recoveryServiceHost, ConfigBaseClass.m_recoveryServicePort);
		}

		public override string ToString()
		{
			string[] array = new string[5];
			Guid txnGuid = m_txnGuid;
			array[0] = txnGuid.ToString();
			array[1] = "==";
			txnGuid = m_rmGuid;
			array[2] = txnGuid.ToString();
			array[3] = "==";
			array[4] = m_branchNum.ToString();
			return string.Concat(array);
		}
	}
}
